import 'package:flutter/material.dart';

import 'EvaluationDialog.dart';
import 'constants.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlueAccent),
        useMaterial3: true,
      ),
      title: '班级素养评测',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<String> _studentList = []; // 初始化学生列表
  String _selectedStudent = ''; // 选中的学生

  void _showAddStudentDialog(BuildContext context) {
    final TextEditingController controller = TextEditingController();

    showDialog(
      context: context,
      builder: (BuildContext context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12.0),
          ),
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text(
                      '添加学生',
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.blue,
                      ),
                    ),
                    IconButton(
                      icon: const Icon(Icons.close),
                      onPressed: () {
                        Navigator.of(context).pop(); // 关闭弹窗
                      },
                    ),
                  ],
                ),
                const SizedBox(height: 20),
                Container(
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.black, width: 1),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: TextField(
                    controller: controller,
                    maxLines: 6,
                    decoration: const InputDecoration(
                      hintText: '请输入学生姓名，每行一个',
                      border: InputBorder.none,
                      contentPadding: EdgeInsets.all(10),
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                SizedBox(
                  width: double.infinity,
                  height: 45,
                  child: ElevatedButton(
                    onPressed: () {
                      final input = controller.text.trim();
                      if (input.isEmpty) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('请输入至少一个学生姓名')),
                        );
                        return;
                      }
                      final lines = input.split('\n');
                      final students = lines
                          .map((e) => e.trim())
                          .where((e) => e.isNotEmpty)
                          .toList();
                      print("数据：$students");
                      setState(() {
                        _studentList.addAll(students);
                        if (_studentList.isNotEmpty) {
                          _selectedStudent = _studentList[0]; // 默认选择第一个学生
                        }
                      });
                      // 这里处理导入逻辑
                      Navigator.of(context).pop();
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.greenAccent[400],
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30),
                      ),
                    ),
                    child: const Text(
                      '导入学生',
                      style: TextStyle(fontSize: 16, color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('班级素养评测')),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(height: 10),
                  Text(
                    '欢迎使用班级素养测评系统',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Colors.blue[900],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 16,
                      top: 10,
                      right: 16,
                      bottom: 16,
                    ),
                    child: Text(
                      '请从下方菜单选择评价维度，对当前学生进行素养评价。完成所有评价后，点击"生成素养报告"按钮获取详细分析报告。',
                      style: TextStyle(fontSize: 14, color: Colors.black87),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ],
              ),
              Wrap(
                spacing: 8, // 标签之间的水平间距
                runSpacing: 4, // 行之间的垂直间距
                children: [
                  // 添加学生
                  IconText(
                    icon: Icons.add,
                    text: '添加学生',
                    onTap: () => _showAddStudentDialog(context),
                  ),
                  // 和润德育
                  IconText(
                    icon: Icons.emoji_people,
                    text: '和润德育',
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) => EvaluationDialog(title: "和润德育"),
                      );
                    },
                  ),
                  // 语文素养
                  IconText(
                    icon: Icons.menu_book,
                    text: '语文素养',
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) => EvaluationDialog(title: "语文素养"),
                      );
                    },
                  ),
                  // 数学素养
                  IconText(icon: Icons.calculate, text: '数学素养'),
                  // 英语素养
                  IconText(icon: Icons.translate, text: '英语素养'),
                  // 科学素养
                  IconText(icon: Icons.science, text: '科学素养'),
                  // 艺术素养
                  IconText(icon: Icons.palette, text: '艺术素养'),
                  // 体育素养
                  IconText(icon: Icons.sports_soccer, text: '体育素养'),
                  // 和乐劳育素养
                  IconText(icon: Icons.construction, text: '和乐劳育素养'),
                  // 心理健康素养
                  IconText(icon: Icons.psychology, text: '心理健康素养'),
                  // 家长评价及建议
                  IconText(icon: Icons.family_restroom, text: '家长评价及建议'),
                  // 期末总评
                  IconText(icon: Icons.grade, text: '期末总评'),
                  // 生成素养报告
                  IconText(icon: Icons.assignment, text: '生成素养报告'),
                  // 清除数据
                  IconText(icon: Icons.delete, text: '清除数据'),
                  // 班级素养分析
                  IconText(icon: Icons.analytics, text: '班级素养分析'),
                ],
              ),
              SizedBox(height: 5),
              if (_studentList.isNotEmpty) ...[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '当前学生：$_selectedStudent',
                      style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.w600,
                        fontSize: 18,
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.all(10),
                      width: double.infinity,
                      // This makes the container take full width
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey), // Border color
                        borderRadius: BorderRadius.circular(
                          8.0,
                        ), // Rounded corners
                      ),
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12.0),
                        child: DropdownButton<String>(
                          isExpanded: true,
                          // This makes the button expand to fill available width
                          value: _selectedStudent.isNotEmpty
                              ? _selectedStudent
                              : null,
                          items: _studentList.map((String student) {
                            return DropdownMenuItem<String>(
                              value: student,
                              child: Text(student),
                            );
                          }).toList(),
                          onChanged: (String? newValue) {
                            setState(() {
                              _selectedStudent = newValue!;
                            });
                          },
                          underline:
                              Container(), // Removes the default underline
                        ),
                      ),
                    ),
                  ],
                ),
              ] else ...[
                const Text(
                  '当前学生：请选择',
                  style: TextStyle(
                    color: Colors.black,
                    fontWeight: FontWeight.w600,
                    fontSize: 18,
                  ),
                ),
              ],
            ],
          ),
        ),
      ),
    );
  }
}

class IconText extends StatelessWidget {
  final IconData icon;
  final String text;
  final GestureTapCallback? onTap;

  const IconText({
    super.key,
    required this.icon,
    required this.text,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Chip(
        avatar: CircleAvatar(
          backgroundColor: Colors.blue.shade100,
          child: Icon(icon, size: 18, color: Colors.blueAccent),
        ),
        label: Text(text),
      ),
    );
  }
}
